var orm = require('orm');
// console.log(orm.singleton.clear())
var express_orm = orm.express(require("./config/mysqlconfig.js"), {
	define: function (db, models) {
		for (var tablename in tables) {
			(function (tablename) {
				var table_info = tables[tablename];
				var id;
				for (var key in table_info) {
					if (!id) {
						key.indexOf("_id") !== -1 && key.split("-").length === 2;
						id = key;
					}
					table_info[key] = typeMap[table_info[key]];
				}
				console.log("ORM定义初始化定义表：", tablename, "\t\t主键：", id);
				models[tablename] = db.define(tablename, table_info, {
					id: id
				}, {
						autoFetch: true, // global eager load
						cache: false
					});
				// console.log(models[tablename]);
			} (tablename));
		}
	}
});
// var Person = db.define('person', {
//     name    : String
// }, {
//     cache   : false
// });

/*
 *  数据库表映射信息
 */
var typeMap = {
	"tinyint": Boolean,
	"int": Number,
	"float": Number,
	"double": Number,
	"varchar": String,
	"text": String,
	"char": String,
	"boolean": Boolean,
}

var tables = {
	"student": {
		"sid": "varchar",
		"name": "varchar",
		"pwd": "varchar",
		"createtime": "varchar",
		"phone": "varchar",
		"sex": "int",
		"education": "int",
		"email": "varchar",
		"address": "varchar",
		"logo": "varchar",
		"nickname": "varchar"
	},
	"teacher": {
		"tid": "varchar",
		"name": "varchar",
		"pwd": "varchar",
		"createtime": "varchar",
		"phone": "varchar",
		"sex": "int",
		"email": "varchar",
		"address": "varchar",
		"logo": "varchar",
		"nickname": "varchar"
	},
	"calendar": {
		"id": "int",
		"start": "varchar",
		"end": "varchar",
		"title": "varchar",
		"allday": "boolean",
		"url": "text",
		"color": "varchar",
		"uid": "varchar",
		"sid": "varchar",
		"tid": "varchar",
		"cid": "int"
	},
	"course": {
		"cid": "int",
		"coursetype": "int",
		"coursename": "varchar",
		"tid": "varchar",
		"tname": "varchar",
		"sid": "varchar"
	},
	"coursecalendar": {
		"id": "varchar",
		"title": "varchar",
		"cid": "varchar",
		"cname": "varchar",
		"tid": "varchar",
		"tname": "varchar",
		"start": "varchar",
		"end": "varchar",
		"color": "varchar"
	},
	"studentcourse": {
		"id": "int",
		"sid": "varchar",
		"tid": "varchar",
		"cid": "int",
		"cname": "varchar",
		"tname": "varchar",
		"ctype": "int"
	},
	"notetype": {
		"id": "varchar",
		"name": "varchar",
		"num": "int",
		"sid": "varchar",
		"tid": "varchar"
	},
	"notes": {
		"id": "varchar",
		"tid": "varchar",
		"title": "varchar",
		"text": "varchar",
		"createtime": "varchar",
		"url": "varchar",
		"src": "varchar",
		"typename": "varchar",
		"sid": "varchar",
		"tid": "varchar",
		"typeid": "varchar"
	},
	"paper": {
		"id": "int",
		"tid": "varchar",
		"title": "varchar",
		"text": "varchar",
		"date": "varchar",
		"tname": "varchar"
	},
	"video": {
		"id": "int",
		"cid": "varchar",
		"date": "varchar",
		"name": "varchar",
		"url": "varchar",
		"tid": "varchar",
		"tname": "varchar",
		"type": "int",
		"sid": "varchar",
		"chat": "varchar"
	},
	"chat": {
		"chatid": "int",
		"myid": "varchar",
		"sid": "varchar",
		"tid": "varchar",
		"chat": "varchar"
	},
	"friends": {
		"fid": "int",
		"chatid": "int",
		"myid": "varchar",
		"sid": "varchar",
		"tid": "varchar",
		"nickname": "varchar",
		"logo": "varchar"
	}
};

module.exports = express_orm;